package com.example.simpdptest;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewParent;
import android.widget.LinearLayout;

public class RotateLayout extends LinearLayout   {
    private static final String TAG = "minimap.RotateLayout";
    Matrix invMat;
    boolean mLockRotation;
    boolean mUseNewImplementation;
    RectF newDirtyF;
    Matrix rotMat;
    private int rotation=0;

    public RotateLayout(Context context) {
	super(context);
	rotMat = new Matrix();
	invMat = new Matrix();
	newDirtyF = new RectF();
	mLockRotation = false;
	mUseNewImplementation = false;


    }

    public RotateLayout(Context paramContext, AttributeSet paramAttributeSet) {

	super(paramContext, paramAttributeSet);

	rotMat = new Matrix();
	invMat = new Matrix();
	newDirtyF = new RectF();
	mLockRotation = false;
	mUseNewImplementation = false;

    }

    public boolean dispatchTouchEvent(MotionEvent paramMotionEvent) {

	float[] arrayOfFloat = new float[2];
	float f1 = paramMotionEvent.getX();
	arrayOfFloat[0] = f1;
	float f2 = paramMotionEvent.getY();
	arrayOfFloat[1] = f2;
	this.rotMat.mapPoints(arrayOfFloat);
	int i = (int) arrayOfFloat[0];
	int j = (int) arrayOfFloat[1];
	paramMotionEvent.setLocation(i, j);
	return super.dispatchTouchEvent(paramMotionEvent);
    }

    public boolean dispatchTrackballEvent(MotionEvent paramMotionEvent) {

	float f1 = -paramMotionEvent.getY();
	float f2 = paramMotionEvent.getX();
	paramMotionEvent.setLocation(f1, f2);
	return super.dispatchTrackballEvent(paramMotionEvent);
    }

    public float getRotation() {
	return this.rotation;
    }

    public ViewParent invalidateChildInParent(int[] paramArrayOfInt,
	    Rect paramRect) {
	int i = paramArrayOfInt[0];
	int j = paramArrayOfInt[1];
	paramRect.offset(i, j);
	this.newDirtyF.set(paramRect);
	Matrix localMatrix = this.invMat;
	RectF localRectF = this.newDirtyF;
	localMatrix.mapRect(localRectF);
	this.newDirtyF.roundOut(paramRect);
	invalidate(paramRect);
	return null;
    }





    protected void onLayout(boolean b, int x, int y, int w, int h) {
	if ((this.rotation == 0) || (this.rotation == 2))
	    super.onLayout(b, y, x, h, w);
	else
	    super.onLayout(b, x, y, w, h);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

	if ((this.rotation == 0) || (this.rotation == 2)) {
	    super.onMeasure(heightMeasureSpec, widthMeasureSpec);

	} else {
	    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}
	rotateMeasureMent();
    }

    protected void dispatchDraw(Canvas canvas) {
	canvas.save();
	Matrix localMatrix = this.invMat;
	canvas.concat(localMatrix);
	super.dispatchDraw(canvas);
	canvas.restore();
    }

    protected void rotateMeasureMent() {

	if ((this.rotation == 0) || (this.rotation == 2)) {
	    int i = getMeasuredHeight();
	    int j = getMeasuredWidth();
	    setMeasuredDimension(i, j);
	}

	this.rotMat.reset();

	switch (this.rotation) {
	case 1: {
	    invMat = new Matrix(rotMat);
	    Matrix localMatrix3 = this.rotMat;
	    Matrix localMatrix4 = this.invMat;
	    localMatrix3.invert(localMatrix4);

	    return;
	}
	case 3: {
	    this.rotMat.setRotate(180.0F);
	    Matrix localMatrix5 = this.rotMat;
	    float f1 = getMeasuredWidth();
	    float f2 = getMeasuredHeight();
	    localMatrix5.postTranslate(f1, f2);

	    invMat = new Matrix(rotMat);
	    Matrix localMatrix3 = this.rotMat;
	    Matrix localMatrix4 = this.invMat;
	    localMatrix3.invert(localMatrix4);
	    return;
	}
	case 0: {
	    rotMat.setRotate(90.0F);
	    float f3 = getMeasuredHeight();
	    rotMat.postTranslate(f3, 0.0F);
	    invMat = new Matrix(rotMat);
	    rotMat.invert(invMat);
	    return;
	}
	case 2: {
	    this.rotMat.setRotate(270.0F);
	    Matrix localMatrix7 = this.rotMat;
	    float f4 = getMeasuredWidth();
	    localMatrix7.postTranslate(0.0F, f4);

	    invMat = new Matrix(rotMat);
	    Matrix localMatrix3 = this.rotMat;
	    Matrix localMatrix4 = this.invMat;
	    localMatrix3.invert(localMatrix4);
	    return;
	}
	}

    }

    public void rotationChanged(int paramInt) {
	setRotation(paramInt);
    }

    public void setRotation(int i) {
	rotation = i;
	requestLayout();
	invalidate();

    }

    public void useNewImplementation(boolean paramBoolean) {
	this.mUseNewImplementation = paramBoolean;
    }
}